/*
 * All rights Reserved, Designed By DataDriver
 * Copyright:    DataDriver.Inc
 * Company:      Zhuo Wo Infomation Technology (ShangHai) CO.LTD
 */
package com.sun.bq;


import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * 阻塞队列：
 * 例如线程A调用线程B,必须线程B完成后线程A才执行，这时线程A就是阻塞
 *
 * 方式 抛出异常 有返回值，不抛出异常 阻塞 等待 超时等待
 * 添加 add offer() put() offer(,,)
 * 移除 remove poll() take() poll(,)
 * 检测队首元素 element peek - -
 */
public class BlockQueneDemo {

    public static void main(String[] args) throws InterruptedException {
       test4();
    }

    /**
     * 抛出异常
     * 例如一个队列的长度是4，当队列满了的时候再次追加就会报错
     * 或者数队列为空时，在取时也会抛出异常
     * ang.IllegalStateException: Queue full
     */
    public static void test1(){
        BlockingQueue blockingQueue = new ArrayBlockingQueue(4);

        blockingQueue.add("1");  //向队列中追加数据
        blockingQueue.add("2");
        blockingQueue.add("3");
        blockingQueue.add("4");
        //blockingQueue.add("5");
        System.out.println("-------------");
        System.out.println(blockingQueue.remove()); //去除队列中的值
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
    }


    /**
     * 有返回值，不抛出异常
     */
    public static void test2(){
        BlockingQueue blockingQueue = new ArrayBlockingQueue(4);

        blockingQueue.offer("1");  //向队列中追加数据
        blockingQueue.offer("2");
        blockingQueue.offer("3");
        blockingQueue.offer("4");
        blockingQueue.offer("5");
        System.out.println("-------------");
        System.out.println(blockingQueue.poll()); //去除队列中的值
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
    }



    /**
     * 等待，阻塞（一直阻塞）
     */
    public static void test3() throws InterruptedException {
        BlockingQueue blockingQueue = new ArrayBlockingQueue(4);

        blockingQueue.put("1");  //向队列中追加数据
        blockingQueue.put("2");
        blockingQueue.put("3");
        blockingQueue.put("4");
        blockingQueue.put("5");
        System.out.println("-------------");
        System.out.println(blockingQueue.take()); //去除队列中的值
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
    }


    /**
     * 等待，阻塞（超时等待）
     */
    public static void test4() throws InterruptedException {
        BlockingQueue blockingQueue = new ArrayBlockingQueue(4);

        blockingQueue.offer("1");  //向队列中追加数据
        blockingQueue.offer("2");
        blockingQueue.offer("3");
        blockingQueue.offer("4");
        blockingQueue.offer("5",2, TimeUnit.SECONDS);
        System.out.println("-------------");
        System.out.println(blockingQueue.poll()); //去除队列中的值
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll(2,TimeUnit.SECONDS));
    }
}
